package com.sann.user.controller;


import com.sann.common.utils.JwtUtil;
import com.sann.common.utils.R;
import com.sann.user.entity.Member;
import com.sann.user.entity.vo.RegisterVo;
import com.sann.user.service.MemberService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author zjw
 * @since 2021-05-24
 */
@RestController
@RequestMapping("/eduucenter/member/*")
@CrossOrigin
@Slf4j
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation(value = "注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.ok();
    }

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public R login(@RequestBody Member member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    @ApiOperation(value = "根据token获取用户信息")
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。从 request对象获取头信息，返回用户id
        String memberId = JwtUtil.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        Member member = memberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }

}

